<?php

require_once 'mp3\PDO.php';
require_once 'mp3/Constants.php';

class mp3_GestionarUsuari
{

	protected $_userId;
	protected $_nom;
	protected $_pdp;
	protected $_email;
	protected $_error;
	protected $_logged;

	const SQL_USUARI_EXISTENT = 'SELECT userId FROM users WHERE nom = :nom';
	const SQL_MAIL_EXISTENT = 'SELECT userId FROM users WHERE mail = :mail';	
	const SQL_LOGIN = 'SELECT userId FROM users WHERE nom = :nom AND pdp = :pdp';
	const SQL_REGISTRAR = 'INSERT INTO users (nom, pdp, mail) VALUES (:nom,:pdp,:mail)';
	const SQL_CANVIAR_PDP = 'UPDATE users SET pdp=:pdp WHERE nom=:nom';
	
	public function __construct ($nom="", $pdp="", $email="") {
		$this->_nom = $nom;
		$this->_pdp = $pdp;
		$this->_email = $email;
		$this->_logged = false;
	}
	
	public function ok(){
		return $this->_logged;
	}
	
	public function getError(){
		return $this->_error;
	}
	
	public function registrar(){
		if ( $this->_email!="" && !($this->nomJaRegistrat()) && !($this->mailJaRegistrat()) )
		{
			//Registrar
			$dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarUsuari::SQL_REGISTRAR);
			$parsedPdp = Mp3_Constants::SALT.$this->_pdp;
			$dbStatement->execute(array(':nom'=>$this->_nom, ':pdp'=>sha1($parsedPdp), ':mail'=>$this->_email));
			$this->login();
			$this->_logged = true;
		} else if ( $this->_email == "" ){
			$this->_error = 'E-mail invalid';
			$this->_logged = false;
		} else {
			$this->_error = 'Usuari invalid';
			$this->_logged = false;
		}
	}

	public function login(){
		if ( $this->_nom != "" && $this->_pdp != "" ){
			if ( $this->usuariCorrecte() ) {
				$this->_logged = true;
			} else {
				$this->_error = 'Usuari o password incorrecte';
				$this->_logged = false;	
			}
		} else {
			$this->_error = 'Totes les dades s�n obligatories';
			$this->_logged = false;
		}
		
	}
	
	public function nomJaRegistrat(){
		$dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarUsuari::SQL_USUARI_EXISTENT);
		$dbStatement->execute(array(':nom'=>$this->_nom));
		$result = $dbStatement->fetch();
		return ($result)?true:false;
	}

	public function mailJaRegistrat()
	{
		$dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarUsuari::SQL_MAIL_EXISTENT);
		$dbStatement->execute(array(':mail'=>$this->_email));
		$result = $dbStatement->fetch();
		return ($result)?true:false;
	}	
	public static function canviPass($username, $nouPdp)
	{
		$dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarUsuari::SQL_CANVIAR_PDP);
		$nouPdp=Mp3_Constants::SALT.$nouPdp;
		$dbStatement->execute(array(':pdp'=>sha1($nouPdp), ':nom'=>$username));
		$result = $dbStatement->fetch();
		return ($result)?true:false;
	}

	private function usuariCorrecte(){
		$dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarUsuari::SQL_LOGIN);
		$parsedPdp = Mp3_Constants::SALT.$this->_pdp;
		$dbStatement->execute(array(':nom'=>$this->_nom, ':pdp'=>sha1($parsedPdp)));
		$result = $dbStatement->fetch();
		$this->_userId=$result['userId'];
		return ($result)?true:false;
	}
	
	public function getUserId(){
		return $this->_userId;
	}
	
	public function getUserName(){
		return $this->_nom;
	}
	
	static public function cercarPerUsername($username){
		$dbStatement = mp3_PDO::getInstance()->prepare(mp3_GestionarUsuari::SQL_USUARI_EXISTENT);
		$dbStatement->execute(array(':nom'=>$username));
		$result = $dbStatement->fetch();
		return $result ['userId'];
	}
	
}